const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
const formatTime2 = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  return [year, month, day].map(formatNumber).join('-')
}
//判断时间格式yy-mm-dd
function strDateTime(str)
{
var r = str.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
if(r==null)return false;
var d= new Date(r[1], r[3]-1, r[4]);
return (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]);
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

// 按钮只能点击一次间隔gapTime
function throttle(fn, gapTime) {
  if (gapTime == null || gapTime == undefined) {
    gapTime = 800
  }
  let _lastTime = null
  // 返回新的函数
  return function() {
    let _nowTime = +new Date()
    if (_nowTime - _lastTime > gapTime || !_lastTime) {
      fn.apply(this, arguments) //将this和参数传给原函数
      _lastTime = _nowTime
    }else{
      console.log("频繁点击")
    }
  }
}


//base64解码

function Base64() {
  // private property
  const _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

  // public method for decoding
  this.decode = function (input) {
    var output = "";
    var chr1, chr2, chr3;
    var enc1, enc2, enc3, enc4;
    var i = 0;
    input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
    while (i < input.length) {
      enc1 = _keyStr.indexOf(input.charAt(i++));
      enc2 = _keyStr.indexOf(input.charAt(i++));
      enc3 = _keyStr.indexOf(input.charAt(i++));
      enc4 = _keyStr.indexOf(input.charAt(i++));
      chr1 = (enc1 << 2) | (enc2 >> 4);
      chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
      chr3 = ((enc3 & 3) << 6) | enc4;
      output = output + String.fromCharCode(chr1);
      if (enc3 != 64) {
        output = output + String.fromCharCode(chr2);
      }
      if (enc4 != 64) {
        output = output + String.fromCharCode(chr3);
      }
    }
    output = _utf8_decode(output);
    return output;
  }

  // private method for UTF-8 decoding
  let _utf8_decode = function (utftext) {
    var string = "";
    var i = 0;
    var c =0, c1 =0, c2 = 0, c3 = 0;
    while (i < utftext.length) {
      c = utftext.charCodeAt(i);
      if (c < 128) {
        string += String.fromCharCode(c);
        i++;
      } else if ((c > 191) && (c < 224)) {
        c2 = utftext.charCodeAt(i + 1);
        string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
        i += 2;
      } else {
        c2 = utftext.charCodeAt(i + 1);
        c3 = utftext.charCodeAt(i + 2);
        string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
        i += 3;
      }
    }
    return string;
  }
}

function base64Decode(str){
  var base = new Base64();
  var result = base.decode(str);
  return result;
}

function scanQRCode(type) {
  let url = '/pages/signInRegister/signInRegister'
  if ("login"== type){
    // console.log(123)
    url = '/pages/signInRegister/signInRegister?login=1'
  }
  wx.scanCode({
    onlyFromCamera: true,
    scanType: "qrCode",
    success: (res) => {
      let qrStr = this.base64Decode(res.result)
      // console.log(typeof qrStr == 'string')
      if (typeof qrStr == 'string') {
        console.log(qrStr)
        try {
          let obj = JSON.parse(qrStr);
          if (typeof obj == 'object' && obj.type && obj.meetingId) {
            wx.setStorageSync("signInQr", obj)
            wx.navigateTo({
              url: url,
            })
          } else {
            wx.showModal({
              content: '无法识别此二维码，请重试！',
              showCancel: false
            })
          }
        } catch (e) {
          wx.showModal({
            content: '无法识别此二维码，请重试！',
            showCancel: false
          })
        }
      }
    }
  })
}


module.exports = {
  formatTime: formatTime,
  formatTime2: formatTime2,
  strDateTime: strDateTime ,
  throttle: throttle,
  base64Decode: base64Decode,
  scanQRCode: scanQRCode
}
